package com.jcrj.action.system;

import com.jcrj.action.BaseAction;
import com.jcrj.entity.OOrg;
import com.jcrj.entity.OUser;
import com.jcrj.service.OrganizationService;
import com.jcrj.service.SysLogService;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;

public class OrganizationAction extends BaseAction {
	private OrganizationService organizationService;
	private List<OOrg> orgList;
	private int orgId;
	private long totalCount;
	private long pageCount;
	private OOrg organization;
	private String name;
	private String pid;
	private String status;
	private SysLogService sysLogService;

	public String organizationManager() {
		return "success";
	}

	public String orgByFlow() {
		return "success";
	}

	public String getOrgListById() {
		this.organization = this.organizationService
				.getOrganization(this.orgId);
		this.totalCount = this.organizationService.getTotalCount(this.orgId)
				.longValue();
		this.pageCount = ((this.totalCount - 1L) / 10L + 1L);
		if (super.getCurrentPage() > this.pageCount) {
			super.setCurrentPage(Long.valueOf(this.pageCount).intValue());
		}
		this.orgList = this.organizationService.getOrgListById(this.orgId,
				super.getCurrentPage());
		return "success";
	}

	public String getOrgByParent() {
		try {
			super.getResponse().setCharacterEncoding("UTF-8");
			OOrg org = new OOrg();
			if ((this.pid != null) && (!"".equals(this.pid))) {
				org = this.organizationService.getOrganization(Integer
						.parseInt(this.pid));
			}
			List list = this.organizationService
					.findByName(this.name, this.pid);
			JSONObject json = new JSONObject();
			json.put("org", org);
			json.put("orgList", list);
			super.getResponse().getWriter().write(json.toString());
			super.getResponse().getWriter().flush();
		} catch (IOException e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public String getOrgByFlow() {
		try {
			super.getResponse().setCharacterEncoding("UTF-8");
			OOrg org = this.organizationService.getOrganization(this.orgId);
			JSONObject json = new JSONObject();
			json.put("org", org);
			super.getResponse().getWriter().write(json.toString());
			super.getResponse().getWriter().flush();
		} catch (IOException e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public String getOrganizationTree() {
		try {
			super.getResponse().setCharacterEncoding("UTF-8");
			String org = this.organizationService.getAllTree();
			super.getResponse().getWriter().write(org);
			super.getResponse().getWriter().flush();
		} catch (IOException e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public String getChildOrgTree() {
		try {
			super.getResponse().setCharacterEncoding("UTF-8");
			String org = this.organizationService.getChildTree(this.orgId);
			super.getResponse().getWriter().write(org);
			super.getResponse().getWriter().flush();
		} catch (IOException e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public String getGrouphome() {
		return "success";
	}

	public String getGrouphomeByPhone() {
		return "success";
	}

	public String getGroupNo() {
		try {
			super.getResponse().getWriter().write("[]");
			super.getResponse().getWriter().flush();
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

	public String getOrgAndGroup() {
		try {
			super.getResponse().setCharacterEncoding("UTF-8");
			OUser user = (OUser) getSession().get("userInfo");
			String group = this.organizationService.getWorkGroup(user.getId()
					.intValue());
			super.getResponse().getWriter().write(group);
			super.getResponse().getWriter().flush();
		} catch (IOException e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public String getOrgandGrouption() {
		try {
			super.getResponse().setCharacterEncoding("UTF-8");
			String org = this.organizationService.getAllTree();
			super.getResponse().getWriter().write(org);
			super.getResponse().getWriter().flush();
		} catch (IOException e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public String addInit() {
		return "success";
	}

	public String addOrganization() {
		try {
			OUser user = (OUser) getSession().get("userInfo");
			this.organization.setCreateTime(new Date());
			this.organization.setDel("0");
			this.organizationService.saveOrg(this.organization);
			this.sysLogService.saveLog("组织管理", user.getName(), "添加了"
					+ this.organization.getName() + "组织信息");
			super.getResponse().getWriter().write("success");
			super.getResponse().getWriter().flush();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public String getOrgObjById() {
		this.organization = this.organizationService
				.getOrganization(this.orgId);
		return "success";
	}

	public String updateOrg() {
		try {
			OUser user = (OUser) getSession().get("userInfo");
			this.organizationService.updateOrg(this.organization);
			this.sysLogService.saveLog("组织管理", user.getName(), "修改了"
					+ this.organization.getName() + "组织信息");
			super.getResponse().getWriter().write("success");
			super.getResponse().getWriter().flush();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public String deleteOrg() {
		try {
			OUser user = (OUser) getSession().get("userInfo");
			this.organizationService.delete(Integer.valueOf(this.orgId));
			this.sysLogService.saveLog("组织管理", user.getName(), "删除了一个组织");
			super.getResponse().getWriter().write("success");
			super.getResponse().getWriter().flush();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public String selectOrg() {
		return "success";
	}

	public OrganizationService getOrganizationService() {
		return this.organizationService;
	}

	public void setOrganizationService(OrganizationService organizationService) {
		this.organizationService = organizationService;
	}

	public List<OOrg> getOrgList() {
		return this.orgList;
	}

	public void setOrgList(List<OOrg> orgList) {
		this.orgList = orgList;
	}

	public int getOrgId() {
		return this.orgId;
	}

	public void setOrgId(int orgId) {
		this.orgId = orgId;
	}

	public long getTotalCount() {
		return this.totalCount;
	}

	public void setTotalCount(long totalCount) {
		this.totalCount = totalCount;
	}

	public long getPageCount() {
		return this.pageCount;
	}

	public void setPageCount(long pageCount) {
		this.pageCount = pageCount;
	}

	public OOrg getOrganization() {
		return this.organization;
	}

	public void setOrganization(OOrg organization) {
		this.organization = organization;
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPid() {
		return this.pid;
	}

	public void setPid(String pid) {
		this.pid = pid;
	}

	public String getStatus() {
		return this.status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public SysLogService getSysLogService() {
		return this.sysLogService;
	}

	public void setSysLogService(SysLogService sysLogService) {
		this.sysLogService = sysLogService;
	}
}